<template>
    <div class="loginDiv">
        <el-form class="loginDiv-form" :model="form" label-width="0px" ref="ruleFormRef" :rules="loginRules">
            <h1 class="loginDiv-form-word">Hello，欢迎登录平台</h1>
            <el-form-item prop="username">
                <el-input v-model="form.username" :prefix-icon="User" clearable />
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="form.password" type="password" show-password :prefix-icon="Lock" clearable />
            </el-form-item>
            <el-form-item>
                <el-button class="elBon" type="primary" @click="loginFun" :loading-icon="Eleme"
                    :loading="btnLoading">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { User, Lock, Eleme } from '@element-plus/icons-vue'
import type { FormRules } from 'element-plus'
import { useStoreUser } from '../../store/modules/user';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { getTimeWord } from '@/utils/time';

// 表单数据类型
interface RuleForm {
    username: string
    password: string
}
// 登录表单
let form: RuleForm = reactive({
    username: '',
    password: '',
})
// 登录按钮加载状态
let btnLoading = ref(false);
// 表单验证
const loginRules = reactive<FormRules<RuleForm>>({
    username: [
        { required: true, message: '用户名不能为空', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '密码不能为空', trigger: 'blur' },
        { min: 6, max: 10, message: '密码长度在6~10位之间', trigger: 'change' },
    ],
})

// 获取form表单
const ruleFormRef: any = ref();
// pinia
const useStore = useStoreUser();
// router
const $router = useRouter();

// 登录方法
const loginFun = async () => {
    await ruleFormRef.value.validate(async (valid: boolean, fields: object) => {
        if (valid) {
            btnLoading.value = true;
            try {
                await useStore.userLogin(form);
                $router.push('/');
                btnLoading.value = false;
                ElMessage({
                    type: 'success',
                    message: '登录成功，' + getTimeWord(),
                })
            } catch (error) {
                btnLoading.value = false;
                ElMessage.error((error as Error).message);
            }
        } else {
            console.log('error submit!', fields)
        }
    })
}

</script>

<style scoped lang="scss">
.loginDiv {
    width: 100%;
    height: 100vh;
    background: url('@/assets/images/beijing.gif') no-repeat;
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: center;

    &-form {
        width: 35%;
        background: url('@/assets/images/loginForm.png') no-repeat;
        background-size: cover;
        padding: 50px;
        margin-bottom: 100px;

        &-word {
            color: #ffffff;
            font-size: 30px;
            font-weight: 600;
            padding: 15px 0 45px 0;
        }

        .elBon {
            width: 100%;
        }
    }
}
</style>